import request from '../utils/require';

interface UploadResponse {
  success: boolean;
  message: string;
  data?: {
    url: string;
    fileName: string;
    fileSize: number;
  };
}

interface DeleteResponse {
  success: boolean;
  message: string;
}

/**
 * 上传文件到腾讯云COS
 * @param {File} file - 要上传的文件对象
 * @param {number} userId - 用户ID
 * @returns {Promise} 返回上传结果
 */
export function uploadFile(file: File, userId: number): Promise<UploadResponse> {
  const formData = new FormData();
  formData.append('file', file);
  formData.append('userId', userId.toString());
  
  // 文件上传需要特殊处理，使用request但修改headers
  return request({
    url: '/api/file/upload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    timeout: 30000 // 上传文件设置更长的超时时间
  });
}

/**
 * 删除文件
 * @param {string} fileUrl - 文件URL
 * @returns {Promise} 返回删除结果
 */
export function deleteFile(fileUrl: string): Promise<DeleteResponse> {
  return request({
    url: '/api/file/delete',
    method: 'delete',
    params: { fileUrl }
  });
}
